<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui/css/layui.css"  media="all">
  <script src="../layui/layui.js" charset="utf-8"></script>
</head>
<body>  
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarDemo">
<div class="demoTable">
  <div class="layui-inline">
    <input class="layui-input" name="id" id="demoReload" placeholder="请输入cs1" autocomplete="off">
  </div>
  <button class="layui-btn" id="btnserach" lay-event="reload">搜索</button>
  <button class="layui-btn layui-btn-xm" lay-event="add">添加</button>
  </div>
</script>

<div id="updatediv" style="display:none">
<form class="layui-form" action="" lay-filter="formTest">
  <div class="layui-form-item">
    <label class="layui-form-label">Cs1</label>
    <div class="layui-input-block">
      <input type="text" name="cs1" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">Cs2</label>
    <div class="layui-input-block">
      <input type="text" name="cs2" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">Cs3</label>
    <div class="layui-input-block">
      <input type="text" name="cs3" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
</div>

 <table class="layui-hide" id="test" lay-filter="test"></table>              

<script>

layui.use(['table','form'], function(){
	var table = layui.table;
	var $ = layui.$;
	var form = layui.form;
	var demotab = table.render({
		elem: '#test'
		,title:'信息测试'
		,url:'../csb/showCsb'
//		,url:'../demo/showAll'
		,toolbar: '#toolbarDemo'
		,cellMinWidth: 80
		,cols: [[
		  {field:'cs1', title: 'Cs1', sort: true}
		  ,{field:'cs2', title: 'Cs2'}
		  ,{field:'cs3', title: 'Cs3', sort: true}
		  ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
		]]
		,page:true
		, done: function(res, curr, count){
			$("#demoReload").val(res.cs1);
		}
    });
  
	table.on('toolbar(test)', function(obj){
        var data = obj.data;
		if(obj.event === "add"){
            var uplayer = layer.open({
                type: 1
                ,anim: 3
                ,content: $('#updatediv')
            });
            form.val("formTest", {
                "cs1": data.cs1
                ,"cs2": data.cs2
                ,"cs3": data.cs3
            });
            form.on('submit(formDemo)',function (data){
                layer.close(uplayer);
                $.ajax({
                    url:"../csb/addCsb"
                    ,type:"post"
                    ,data:data.field
                    ,success:function (d){
                        if(d === 1){
                            layer.msg('添加已成功！！！', {
                                icon: 1,
                                time: 1500
                            }, function(){
                                demotab.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                            });
                        }
                    }
                });
                return false;
            });
		}else if(obj.event === "reload"){
		 //执行重载
	      demotab.reload({
	        page: {
	          curr: 1
	        }
	        ,where: {
	          cs1: $('#demoReload').val()
	        }
	      });
		}
	});
	table.on('tool(test)', function(obj){
		 var data = obj.data;
		if(obj.event === 'del'){
		   layer.confirm('真的删除行么', function(index){
			$.ajax({
				url:'../csb/delCsb/'+data.cs1,
				type:'delete',
				success:function (d){
					if(d == true){
						layer.msg('删除已成功！！！', {
						  icon: 1,
						  time: 1500
						}, function(){
							demotab.reload({
								page: {
					         		 curr: 1
				      			  }
						        ,where: {
						          cs1: null
						        }
					        });
						}); 
					}
				}
			});
			
		  });
		}else if(obj.event === 'edit'){
			var uplayer = layer.open({
			  type: 1
			  ,anim: 3
			  ,content: $('#updatediv')
			});
			form.val("formTest", {
			  "cs1": data.cs1
			  ,"cs2": data.cs2
			  ,"cs3": data.cs3
			});
			form.on('submit(formDemo)',function (data){
				layer.close(uplayer);
				$.ajax({
					url:"../csb/updCsb"
					,type:"post"
					,data:data.field
					,success:function (d){
						console.log(d,typeof(d));
						if(d === 1){
							layer.msg('修改已成功！！！', {
							  icon: 1,
							  time: 1500
							}, function(){
								demotab.reload({
									page: {
						         		 curr: 1 //重新从第 1 页开始
					      			  }
							        ,where: {
							          cs1: null
							        }
						        });
							}); 
						}
					}
				});
				return false;
			});
		}
	});
});
</script>

</body>
</html>